﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Interfaces.IngresoEgreso
{
    public partial class VerIE : System.Web.UI.Page
    {
        private int horas
        {
            set { ViewState["horas"] = value; }
            get { return (int)ViewState["horas"]; }

        }
        private int minutos
        {
            set { ViewState["minutos"] = value; }
            get { return (int)ViewState["minutos"]; }

        }
        private int gestion
        {
            set { ViewState["gest"] = value; }
            get { return (int)ViewState["gest"]; }

        }
        private long evento
        {
            set { ViewState["even"] = value; }
            get { return (long)ViewState["even"]; }

        }
        private float inicial
        {
            set { ViewState["ini"] = value; }
            get { return float.Parse(ViewState["ini"].ToString()); }
        }
        public void Bind()
        {
            DataList1.DataSource = Processes.GestorIngresoEgreso.mostrarIE(Request.QueryString["Gestion"], Request.QueryString["Evento"]);
            DataList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            if (!IsPostBack)
            {
                if (Request.QueryString["Gestion"] != null)
                {
                    if (Request.QueryString["Evento"] != null)
                    {
                        try
                        {
                            gestion = Convert.ToInt32(Request.QueryString["Gestion"]);
                            evento = Convert.ToInt64(Request.QueryString["Evento"]);
                            Bind();

                        }
                        catch (Exception)
                        {
                        }
                    }
                }

            }
            
            horas = Convert.ToInt32(DropDownList2.SelectedValue);
            minutos = Convert.ToInt32(DropDownList3.SelectedValue);
            Label6.Visible = false;
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Eliminar")
            {
                try
                {
                    Label l = (Label)e.Item.FindControl("Nombre");
                    Label t = (Label)e.Item.FindControl("Total"); 
                    Processes.GestorIngresoEgreso.eliminarIE(l.Text,t.Text,gestion,evento);
                    Bind();
                }
                catch (Exception)
                { 
                
                }
            }
            else if (e.CommandName == "Editar")
            {
                Label n = (Label)e.Item.FindControl("Nombre");
                Label v = (Label)e.Item.FindControl("Valor");
                Label c = (Label)e.Item.FindControl("Cantidad");
                Label f = (Label)e.Item.FindControl("Fecha");
                Label t = (Label)e.Item.FindControl("Total");
                inicial = Convert.ToInt64(t.Text);
                Nombre.Text = n.Text;
                TextBox15.Text = v.Text;
                TextBox16.Text = c.Text;
                Actual.Text = f.Text;
                MultiView1.ActiveViewIndex = 1;
            }
           
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(TextBox3.Text))
                    Processes.GestorIngresoEgreso.Actualizar(Nombre.Text, TextBox15.Text, TextBox16.Text, Actual.Text, 0, 0, false,gestion,evento,inicial);
                else
                    Processes.GestorIngresoEgreso.Actualizar(Nombre.Text, TextBox15.Text, TextBox16.Text, TextBox3.Text, horas, minutos, true, gestion, evento, inicial);
                Bind();
                MultiView1.ActiveViewIndex = 0;
                TextBox3.Text = "";
                DropDownList2.SelectedIndex = 0;
                DropDownList3.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                Label6.Visible = true;
                Label6.Text = ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Response.Redirect("ModificarIngresoEgreso.aspx?Gestion="+gestion+"&Tipo=I");
        }
    }
}